package com.swallowtech.mstation.core.service.imp;

import java.util.Collection;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swallowtech.mstation.core.domain.Bookmark;
import com.swallowtech.mstation.core.domain.MusicBookmark;
import com.swallowtech.mstation.core.domain.Tag;
import com.swallowtech.mstation.core.domain.TagItem;
import com.swallowtech.mstation.core.domain.User;
import com.swallowtech.mstation.core.repository.MusicBookmarkRepository;
import com.swallowtech.mstation.core.repository.TagItemRepository;
import com.swallowtech.mstation.core.repository.TagRepository;
import com.swallowtech.mstation.core.service.BookmarkService;

@Service
public class MusicBookmarkServiceImpl implements BookmarkService {
	@Autowired
	MusicBookmarkRepository musicBookmarkRepository;
	@Autowired
	TagRepository tagRepository;
	@Autowired
	TagItemRepository tagItemRepository;
	
	public void saveBookmark(Bookmark bookmark,HashMap<String, Tag> userAllTags) {
		
		String userId = bookmark.getUserId();
		Bookmark dbBookmark = musicBookmarkRepository.findByUniqueKey(userId, bookmark.getUrl());
		
		if(dbBookmark==null){
			musicBookmarkRepository.insert((MusicBookmark)bookmark);			
		}else{
			musicBookmarkRepository.update((MusicBookmark)bookmark);
		}

		
		for(Tag tag:bookmark.getTags()){
			if(!userAllTags.containsKey(tag.getName())){
				tagRepository.insert(tag);
			}
			
			TagItem item = new TagItem();
			item.setId("00001");
			item.setTagId(tag.getId());
			item.setBookmarkId(bookmark.getId());
			item.setSortOrder(1);
			
			tagItemRepository.insert(item);
		}		
		

	}

	public void removeBookmark(Bookmark bookmark) {
		// TODO Auto-generated method stub
		
	}

	public Collection<Bookmark> getBookmarks(User user) {
		// TODO Auto-generated method stub
		return null;
	}

}
